package com.zzj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zzj.bean.DeptBean;
import com.zzj.bean.EmployeeBean;
import com.zzj.bean.TeamBean;
import com.zzj.dao.DeptDao;
import com.zzj.dao.EmpDao;
import com.zzj.dao.TeamDao;
import com.zzj.service.EmpService;
import com.zzj.utils.Result;
import com.zzj.utils.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpDao empDao;
    @Autowired
    private DeptDao deptDao;
    @Autowired
    private TeamDao teamDao;

    /**
     * 添加一名员工
     * @param employeeBean
     * @return
     */
    @Override
    public Result addEmp(EmployeeBean employeeBean) {
        int i = empDao.addEmp(employeeBean);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "添加成功");
        }
        return new Result(false, StatusCode.ERROR, "添加失败");
    }

    /**
     * 通过员工ID删除一名员工
     * @param eid
     * @return
     */
    @Override
    public Result delEmp(int eid) {
        int i = empDao.delEmp(eid);
        if (i == 1) {
            return new Result(true, StatusCode.OK, "删除成功");
        }
        return new Result(false, StatusCode.ERROR, "删除失败");
    }

    /**
     * 通过ID修改一名员工的信息
     * @param employeeBean
     * @return
     */
    @Override
    public Result updateEmp(EmployeeBean employeeBean) {
        int i = empDao.updateEmp(employeeBean);
        TeamBean teamBean = employeeBean.getTeamBean();
        int i1 = teamDao.updTeam(teamBean);
        DeptBean deptBean = teamBean.getDeptBean();
        int i2 = deptDao.updateDeptByDid(deptBean);
        if (i2 == 1) {
            return new Result(true, StatusCode.OK, "修改成功");
        }
        return new Result(false, StatusCode.ERROR, "修改失败");
    }

    /**
     * 分页查看所有员工的信息
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public Result<PageInfo<EmployeeBean>> findAllEmpl(int pageNum, int pageSize) {
            PageHelper.startPage(pageNum,pageSize);
            List<EmployeeBean> allEmpl = empDao.findAllEmpl();
            PageInfo<EmployeeBean> info = new PageInfo<>(allEmpl);
            if (info.getSize()!=0) {
                return new Result<>(true, StatusCode.OK, "查询成功", info);
            }
        return new Result<>(false, StatusCode.ERROR, "查询失败");
    }

    /**
     * 通过ID查看某一位员工信息
     * @param eid
     * @return
     */
    @Override
    public Result<EmployeeBean> findEmpByEid(int eid) {
        Result<EmployeeBean> empByEid = findEmpByEid(eid);
        if (empByEid != null) {
            return new Result<>(true, StatusCode.OK, "查询成功", empByEid);
        }
        return new Result<>(false, StatusCode.ERROR, "查询失败");
    }
}

